/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/*
 * SRCMain.java
 *
 * Created on May 25, 2010, 12:54:42 PM
 */
package UserInterface;

import Controller.SRCFacade;
import Exceptions.ConfigFileNotLoadedException;
import java.io.File;
import java.io.FileNotFoundException;
import javax.swing.JFrame;
import javax.swing.JOptionPane;
import javax.swing.SwingUtilities;
import javax.swing.UIManager;
import javax.swing.UnsupportedLookAndFeelException;

/**
 *
 * @author subx
 */
public class SRCMain extends JFrame {
    private static SRCMain instance = null;
    static private String LOOKANDFEEL = "com.sun.java.swing.plaf.gtk.GTKLookAndFeel";

    private SRCFacade facade = SRCFacade.getInstance();

    private String[] infos;
    private MapCanvas mapCanvas = null;


    /** Creates new form SRCMain */
    private SRCMain() {
        try {
            //Modifica o Look and Feel da janela.
            UIManager.setLookAndFeel(LOOKANDFEEL);
            SwingUtilities.updateComponentTreeUI(this);

            //Referência para a facade
            facade = (SRCFacade) SRCFacade.getInstance();
            
            initComponents();
        } catch (ClassNotFoundException e) {
            System.err.println("Couldn't find class for look and feel: "
                    + LOOKANDFEEL);
            System.err.println("Using default look and feel.");
        } catch (UnsupportedLookAndFeelException e) {
            System.err.println("Can't use the specified look and feel ("
                    + LOOKANDFEEL + ") in this plataform.");
            System.err.println("Using default look and feel.");
        } catch (Exception e) {
            System.err.println("Couldn't get specified look and feel ("
                    + LOOKANDFEEL + ") for some reason.");
            System.err.println("Using default look and feel.");
            e.printStackTrace();
        }
    }

    public static SRCMain getInstance() {
        if(instance == null)
            instance = new SRCMain();

        return instance;
    }
    
    public void setInfos(String[] s){
        this.map.setText(s[0]);
        this.author.setText(s[1]);
        this.city.setText(s[2]);
        this.state.setText(s[3]);
        this.nodes.setText(s[5]);
        this.edges.setText(s[6]);

        this.mapCanvas1.loadMap(s[4]);
    }

    public void addMap(MapCanvas m){
        this.mapCanvas = m;
        this.getContentPane().add(mapCanvas);
    }
    
    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        logo = new javax.swing.JLabel();
        jPanel1 = new javax.swing.JPanel();
        jLabel2 = new javax.swing.JLabel();
        distMax = new javax.swing.JTextField();
        start = new javax.swing.JButton();
        jPanel3 = new javax.swing.JPanel();
        jLabel3 = new javax.swing.JLabel();
        jLabel4 = new javax.swing.JLabel();
        jLabel5 = new javax.swing.JLabel();
        jLabel6 = new javax.swing.JLabel();
        jLabel8 = new javax.swing.JLabel();
        jLabel7 = new javax.swing.JLabel();
        jLabel10 = new javax.swing.JLabel();
        map = new javax.swing.JLabel();
        author = new javax.swing.JLabel();
        city = new javax.swing.JLabel();
        state = new javax.swing.JLabel();
        nodes = new javax.swing.JLabel();
        edges = new javax.swing.JLabel();
        jTabbedPane1 = new javax.swing.JTabbedPane();
        mapCanvas1 = new UserInterface.MapCanvas();
        jPanel2 = new javax.swing.JPanel();
        jScrollPane1 = new javax.swing.JScrollPane();
        jList1 = new javax.swing.JList();
        jMenuBar1 = new javax.swing.JMenuBar();
        jMenu1 = new javax.swing.JMenu();
        startItem = new UserInterface.SRCMenuItem();
        loadConfigItem = new UserInterface.SRCMenuItem();
        unloadConfigItem = new UserInterface.SRCMenuItem();
        jSeparator1 = new javax.swing.JPopupMenu.Separator();
        exitItem = new UserInterface.SRCMenuItem();
        jMenu2 = new javax.swing.JMenu();
        configsItem = new UserInterface.SRCMenuItem();
        logItem = new UserInterface.SRCMenuItem();
        jMenu3 = new javax.swing.JMenu();
        siteItem = new UserInterface.SRCMenuItem();
        docItem = new UserInterface.SRCMenuItem();
        aboutItem = new UserInterface.SRCMenuItem();

        setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
        setTitle("SRC - Sistema de Roteamento de Carteiros");
        setResizable(false);

        logo.setIcon(new javax.swing.ImageIcon("/home/subx/NetBeansProjects/ProjetoSRC/images/SRC-Logo.png")); // NOI18N

        jPanel1.setBorder(javax.swing.BorderFactory.createTitledBorder("Configurações"));

        jLabel2.setFont(new java.awt.Font("DejaVu Sans", 1, 11));
        jLabel2.setText("Distância Máx:");

        start.setText("Iniciar");
        start.setActionCommand("start");
        start.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                startActionPerformed(evt);
            }
        });

        javax.swing.GroupLayout jPanel1Layout = new javax.swing.GroupLayout(jPanel1);
        jPanel1.setLayout(jPanel1Layout);
        jPanel1Layout.setHorizontalGroup(
            jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel1Layout.createSequentialGroup()
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, jPanel1Layout.createSequentialGroup()
                        .addContainerGap()
                        .addComponent(start, javax.swing.GroupLayout.DEFAULT_SIZE, 165, Short.MAX_VALUE))
                    .addGroup(jPanel1Layout.createSequentialGroup()
                        .addComponent(jLabel2)
                        .addGap(18, 18, 18)
                        .addComponent(distMax, javax.swing.GroupLayout.PREFERRED_SIZE, 62, javax.swing.GroupLayout.PREFERRED_SIZE)))
                .addContainerGap())
        );
        jPanel1Layout.setVerticalGroup(
            jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel1Layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel2)
                    .addComponent(distMax, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addGap(58, 58, 58)
                .addComponent(start)
                .addContainerGap())
        );

        jPanel3.setBorder(javax.swing.BorderFactory.createTitledBorder("Informações"));

        jLabel3.setFont(new java.awt.Font("DejaVu Sans", 1, 11));
        jLabel3.setText("Mapa:");

        jLabel4.setFont(new java.awt.Font("DejaVu Sans", 1, 11));
        jLabel4.setText("Autor:");

        jLabel5.setFont(new java.awt.Font("DejaVu Sans", 1, 11));
        jLabel5.setText("Cidade:");

        jLabel6.setFont(new java.awt.Font("DejaVu Sans", 1, 11));
        jLabel6.setText("Estado:");

        jLabel8.setFont(new java.awt.Font("DejaVu Sans", 1, 11));
        jLabel8.setText("Bairro:");

        jLabel7.setFont(new java.awt.Font("DejaVu Sans", 1, 11));
        jLabel7.setText("Número de Vértices:");

        jLabel10.setFont(new java.awt.Font("DejaVu Sans", 1, 11));
        jLabel10.setText("Número Arestas:");

        javax.swing.GroupLayout jPanel3Layout = new javax.swing.GroupLayout(jPanel3);
        jPanel3.setLayout(jPanel3Layout);
        jPanel3Layout.setHorizontalGroup(
            jPanel3Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel3Layout.createSequentialGroup()
                .addGroup(jPanel3Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(jPanel3Layout.createSequentialGroup()
                        .addComponent(jLabel3)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(map))
                    .addGroup(jPanel3Layout.createSequentialGroup()
                        .addComponent(jLabel4)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(author))
                    .addGroup(jPanel3Layout.createSequentialGroup()
                        .addComponent(jLabel5)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(city))
                    .addGroup(jPanel3Layout.createSequentialGroup()
                        .addGap(49, 49, 49)
                        .addComponent(state))
                    .addComponent(jLabel8)
                    .addGroup(jPanel3Layout.createSequentialGroup()
                        .addComponent(jLabel7)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(nodes))
                    .addGroup(jPanel3Layout.createSequentialGroup()
                        .addComponent(jLabel10)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(edges))
                    .addComponent(jLabel6))
                .addContainerGap(54, Short.MAX_VALUE))
        );
        jPanel3Layout.setVerticalGroup(
            jPanel3Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel3Layout.createSequentialGroup()
                .addGroup(jPanel3Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel3)
                    .addComponent(map))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(jPanel3Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel4)
                    .addComponent(author))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(jPanel3Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel5)
                    .addComponent(city))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(jPanel3Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(state)
                    .addComponent(jLabel6))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(jLabel8)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(jPanel3Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel7)
                    .addComponent(nodes))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(jPanel3Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel10)
                    .addComponent(edges))
                .addContainerGap(56, Short.MAX_VALUE))
        );

        jTabbedPane1.addTab("Mapa", mapCanvas1);

        javax.swing.GroupLayout jPanel2Layout = new javax.swing.GroupLayout(jPanel2);
        jPanel2.setLayout(jPanel2Layout);
        jPanel2Layout.setHorizontalGroup(
            jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGap(0, 694, Short.MAX_VALUE)
        );
        jPanel2Layout.setVerticalGroup(
            jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGap(0, 597, Short.MAX_VALUE)
        );

        jTabbedPane1.addTab("Carteiros", jPanel2);

        jList1.setModel(new javax.swing.AbstractListModel() {
            String[] strings = { "Item 1", "Item 2", "Item 3", "Item 4", "Item 5" };
            public int getSize() { return strings.length; }
            public Object getElementAt(int i) { return strings[i]; }
        });
        jScrollPane1.setViewportView(jList1);

        jTabbedPane1.addTab("Resultados", jScrollPane1);

        jMenu1.setText("Arquivo");

        startItem.setText("Iniciar");
        startItem.setActionCommand("start");
        jMenu1.add(startItem);

        loadConfigItem.setText("Carregar Configurações");
        loadConfigItem.setActionCommand("load");
        loadConfigItem.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                loadConfigItemActionPerformed(evt);
            }
        });
        jMenu1.add(loadConfigItem);

        unloadConfigItem.setText("Descarregar Configurações");
        unloadConfigItem.setActionCommand("unload");
        unloadConfigItem.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                unloadConfigItemActionPerformed(evt);
            }
        });
        jMenu1.add(unloadConfigItem);
        jMenu1.add(jSeparator1);

        exitItem.setText("Sair");
        exitItem.setActionCommand("exit");
        exitItem.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                exitItemActionPerformed(evt);
            }
        });
        jMenu1.add(exitItem);

        jMenuBar1.add(jMenu1);

        jMenu2.setText("Exibir");

        configsItem.setText("Configurações");
        configsItem.setActionCommand("configs");
        jMenu2.add(configsItem);

        logItem.setText("Log");
        logItem.setActionCommand("log");
        jMenu2.add(logItem);

        jMenuBar1.add(jMenu2);

        jMenu3.setText("Ajuda");

        siteItem.setText("SRC no Google Code");
        siteItem.setActionCommand("site");
        jMenu3.add(siteItem);

        docItem.setText("Documentação");
        docItem.setActionCommand("documentation");
        jMenu3.add(docItem);

        aboutItem.setText("Sobre");
        aboutItem.setActionCommand("about");
        jMenu3.add(aboutItem);

        jMenuBar1.add(jMenu3);

        setJMenuBar(jMenuBar1);

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(layout.createSequentialGroup()
                        .addGap(33, 33, 33)
                        .addComponent(logo))
                    .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                        .addContainerGap()
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                            .addComponent(jPanel3, javax.swing.GroupLayout.Alignment.LEADING, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                            .addComponent(jPanel1, javax.swing.GroupLayout.Alignment.LEADING, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(jTabbedPane1, javax.swing.GroupLayout.PREFERRED_SIZE, 698, javax.swing.GroupLayout.PREFERRED_SIZE)))
                .addGap(18, 18, 18))
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(layout.createSequentialGroup()
                        .addComponent(jTabbedPane1, javax.swing.GroupLayout.DEFAULT_SIZE, 625, Short.MAX_VALUE)
                        .addContainerGap())
                    .addGroup(layout.createSequentialGroup()
                        .addComponent(logo, javax.swing.GroupLayout.PREFERRED_SIZE, 145, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(jPanel3, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(jPanel1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))))
        );

        pack();
    }// </editor-fold>//GEN-END:initComponents

    private void loadConfigItemActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_loadConfigItemActionPerformed
        // TODO add your handling code here:
        File f;
        SRCFileChooser c = new SRCFileChooser();
        
        c.showFileChooser(this);
        c.loadFile();

        f = c.getInput();
        
        try {
            facade.loadConfig(f);
            setInfos(facade.getInfos());
            mapCanvas1.loadEdges(facade.getEdges());
            mapCanvas1.loadNodes(facade.getNodes());
        } catch (ConfigFileNotLoadedException ex) {
            AlertWindow alert = new AlertWindow();

            alert.showAlertWindow(this, "Falta configurações!", ex);
        } catch (FileNotFoundException ex) {
            AlertWindow alert = new AlertWindow();

            alert.showAlertWindow(this, "Arquivo não encontrado", ex);
        }
    }//GEN-LAST:event_loadConfigItemActionPerformed

    private void exitItemActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_exitItemActionPerformed
        // TODO add your handling code here:
        this.setVisible(false);
        this.dispose();
    }//GEN-LAST:event_exitItemActionPerformed

    private void unloadConfigItemActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_unloadConfigItemActionPerformed
        try {
            // TODO add your handling code here:
            String[] s = {"", "", "", "", "", "", "", "", ""};

            facade.disposeConfig();
            this.mapCanvas1.unload();
            setInfos(s);
        } catch (ConfigFileNotLoadedException ex) {
            AlertWindow alert = new AlertWindow();

            alert.showAlertWindow(SRCMain.getInstance(),
                                  "Falha ao descarregar configurações!",
                                  ex);
        }
    }//GEN-LAST:event_unloadConfigItemActionPerformed

    private void startActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_startActionPerformed
        // TODO add your handling code here:
    }//GEN-LAST:event_startActionPerformed

    /**
     * @param args the command line arguments
     */
    public static void main(String args[]) {
        Object[] possibilities = {"GTK+", "Metal", "Motif"};
        String gtk = "com.sun.java.swing.plaf.gtk.GTKLookAndFeel";
        String metal = "javax.swing.plaf.metal.MetalLookAndFeel";
        String motif = "com.sun.java.swing.plaf.motif.MotifLookAndFeel";
        
        String userOption = (String) JOptionPane.showInputDialog(
                                        null,
                                        "Qual seu look and feel predileto?",
                                        "Look and Feel",
                                        JOptionPane.PLAIN_MESSAGE,
                                        null,
                                        possibilities,
                                        "GTK+");

        if(userOption.equals("GTK+"))
            SRCMain.LOOKANDFEEL = gtk;
        else if(userOption.equals("Metal"))
            SRCMain.LOOKANDFEEL = metal;
        else
            SRCMain.LOOKANDFEEL = motif;

        java.awt.EventQueue.invokeLater(new Runnable() {

            public void run() {
                new SRCMain().setVisible(true);
            }
        });
    }
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private UserInterface.SRCMenuItem aboutItem;
    private javax.swing.JLabel author;
    private javax.swing.JLabel city;
    private UserInterface.SRCMenuItem configsItem;
    private javax.swing.JTextField distMax;
    private UserInterface.SRCMenuItem docItem;
    private javax.swing.JLabel edges;
    private UserInterface.SRCMenuItem exitItem;
    private javax.swing.JLabel jLabel10;
    private javax.swing.JLabel jLabel2;
    private javax.swing.JLabel jLabel3;
    private javax.swing.JLabel jLabel4;
    private javax.swing.JLabel jLabel5;
    private javax.swing.JLabel jLabel6;
    private javax.swing.JLabel jLabel7;
    private javax.swing.JLabel jLabel8;
    private javax.swing.JList jList1;
    private javax.swing.JMenu jMenu1;
    private javax.swing.JMenu jMenu2;
    private javax.swing.JMenu jMenu3;
    private javax.swing.JMenuBar jMenuBar1;
    private javax.swing.JPanel jPanel1;
    private javax.swing.JPanel jPanel2;
    private javax.swing.JPanel jPanel3;
    private javax.swing.JScrollPane jScrollPane1;
    private javax.swing.JPopupMenu.Separator jSeparator1;
    private javax.swing.JTabbedPane jTabbedPane1;
    private UserInterface.SRCMenuItem loadConfigItem;
    private UserInterface.SRCMenuItem logItem;
    private javax.swing.JLabel logo;
    private javax.swing.JLabel map;
    private UserInterface.MapCanvas mapCanvas1;
    private javax.swing.JLabel nodes;
    private UserInterface.SRCMenuItem siteItem;
    private javax.swing.JButton start;
    private UserInterface.SRCMenuItem startItem;
    private javax.swing.JLabel state;
    private UserInterface.SRCMenuItem unloadConfigItem;
    // End of variables declaration//GEN-END:variables
}
